package com.zjdiepu.www.biz.cw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.AddedStatus1;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.dao.cw.CwRefundCarMapper;
import com.zjdiepu.www.dao.cw.CwRemitCarMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.cw.ICwRefundCarService;
import com.zjdiepu.www.model.cw.dto.CwRefundCar;
import com.zjdiepu.www.model.cw.dto.CwRemitCar;
import com.zjdiepu.www.model.cw.ext.CwRefundCarExt;
import com.zjdiepu.www.model.cw.ext.CwRemitCarExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;

/**
 * 退提车款Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月15日 11时14分<br/>
 * @since 1.0<br/>
 */
@Service
public class CwRefundCarServiceImpl extends BaseService implements ICwRefundCarService {
	public static final Logger logger = LoggerFactory.getLogger(CwRefundCarServiceImpl.class);

	@Autowired
	private CwRefundCarMapper cwRefundCarMapper;
	@Autowired
	private CwRemitCarMapper cwRemitCarMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	
	@Override
	public ApiResponse saveRefundCar(CwRefundCar cwRefundCar,
			AbstractSysUser currentUser) {
		String busiId = cwRefundCar.getBusiId();
		CwRemitCarExt cwRemitCarExt = new CwRemitCarExt();
		cwRemitCarExt.setBaseWhere();
		cwRemitCarExt.withOutPage();
		cwRemitCarExt.setBusiId(cwRefundCar.getBusiId());
		List<CwRemitCar> cwRemitCars = cwRemitCarMapper.selectByExt(cwRemitCarExt);
		if(CollectionUtils.isEmpty(cwRemitCars)){
			return ApiResponse.fail(999,"没有垫款，不能完成退款操作");
		}
		
		String remitId = null;
		for(CwRemitCar cwRemitCar : cwRemitCars) {
			String tempRemitId = cwRemitCar.getId();
			CwRefundCarExt cwRefundCarExt = new CwRefundCarExt();
			cwRefundCarExt.setBusiId(busiId);
			cwRefundCarExt.setRemitId(tempRemitId);
			cwRefundCarExt.setBaseWhere();
			int count = cwRefundCarMapper.countByExt(cwRefundCarExt);
			if(count == 0) {
				remitId = tempRemitId;
				break;
			}
		}
		
		if(StringUtils.isEmpty(remitId)) {
			return ApiResponse.fail(999,"已经完成退款，无法再退款");
		}
		
		cwRefundCar.initInsertData(currentUser.getId(), currentUser.getName());
		cwRefundCar.setRemitId(remitId);
		if(!cwRefundCar.isCompleted()) {
			return ApiResponse.fail(999,"退提车款资料不完整");
		}
		cwRefundCarMapper.insert(cwRefundCar);
		
//		YwBusiness ywBusiness = ywBusinessMapper.selectByPrimaryKey(busiId);
//		ywBusiness.setBusiStatus(BusiStatus.YI_TUI_KUAN.getCode());
		YwBusiness ywBusiness = new YwBusiness();
		ywBusiness.setId(busiId);
		ywBusiness.setAddedStatus1(AddedStatus1.YI_TUI_KUAN.getCode());
		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		Set<String> emptyFields = CollectionInitUtil.emptySet();
		emptyFields.add("LOAN_TIME");
		ywBusinessMapper.clearEmpty(emptyFields, busiId);
		
		return ApiResponse.success(cwRefundCar.getId());
	}

	@Override
	public String queryHistoryByBusiId(String busiId) {
		return cwRefundCarMapper.queryHistoryByBusiId(busiId);
	}
	
	@Override
	public void saveUpdateAndDelete(CwRefundCar cwRefundCar, OperateType operType) {
		switch (operType) {
		case add:
			cwRefundCar.initInsertData(cwRefundCar.getModifier(), cwRefundCar.getModifierName());
			cwRefundCarMapper.insert(cwRefundCar);
			break;
		case edit:
			cwRefundCar.initUpdateData(cwRefundCar.getModifier(), cwRefundCar.getModifierName());
			// 影响的记录数
			int updateCount = cwRefundCarMapper.updateByPrimaryKeySelective(cwRefundCar);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			cwRefundCar.initDeleteData(cwRefundCar.getModifier(), cwRefundCar.getModifierName());
			// 影响的记录数
			int deleteCount = cwRefundCarMapper.updateByPrimaryKeySelective(cwRefundCar);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<CwRefundCar> selectByExt(CwRefundCarExt cwRefundCarExt) {
		return cwRefundCarMapper.selectByExt(cwRefundCarExt);
	}
	
	@Override
	public int countByExt(CwRefundCarExt cwRefundCarExt){
		return cwRefundCarMapper.countByExt(cwRefundCarExt);
	}
	
	@Override
	public CwRefundCar selectById(String id) {
		return cwRefundCarMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			cwRefundCarMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(CwRefundCar cwRefundCar) {
		return cwRefundCarMapper.countRefined(cwRefundCar);
	}

	@Override
	public CwRefundCar selectRefined(CwRefundCar cwRefundCar) {
		return cwRefundCarMapper.selectRefined(cwRefundCar);
	}
	
	@Override
	public int deletedRefined(CwRefundCar cwRefundCar) {
		return cwRefundCarMapper.deletedRefined(cwRefundCar);
	}

	@Override
	public boolean repeat(CwRefundCar cwRefundCar) {
		int count = countRefined(cwRefundCar);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(cwRefundCar.getId())) {
			return true;
		}
		CwRefundCar dbItem = selectRefined(cwRefundCar);
		if(null == dbItem) {
			return false;
		}
		return !cwRefundCar.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return cwRefundCarMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<CwRefundCar> cwRefundCars) {
		return cwRefundCarMapper.insertBatch(cwRefundCars);
	}

}
